package com.useeinfo.oa.modules.logger.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.logger.entity.DataCatLog;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther: bin.liu
 * @Date: 2018/12/25 10:48
 * @Description:
 */
@Repository("dataCatLogDao")
public class DataCatLogDao implements CrudDao<DataCatLog> {

    private final static Logger logger = LoggerFactory.getLogger(FacilityDataLogDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("dcl.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String enterpriseId = queryHash.get("enterpriseId");
        if (!StringUtils.isEmpty(enterpriseId)) {
            conditionHash.put("(dcl.enterpriseId = ?{paramIndex}) ", StringConverters.ToLong(enterpriseId));
        }
        String content = queryHash.get("content");
        if (!StringUtils.isEmpty(content)) {
            conditionHash.put("(dcl.content like ?{paramIndex}) ", "%" + content + "%");
        }
        String dataCatName = queryHash.get("dataCatName");
        if (!StringUtils.isEmpty(dataCatName)) {
            conditionHash.put("(dcl.dataCatName like ?{paramIndex}) ", "%" + dataCatName + "%");
        }
        String startTime = queryHash.get("startTime");
        if (!StringUtils.isEmpty(startTime)) {
            conditionHash.put("(dcl.createDate >= ?{paramIndex}) ", DateTimeUtils.parse("yyyy-MM-dd HH:mm",startTime));
        }
        String endTime = queryHash.get("endTime");
        if (!StringUtils.isEmpty(endTime)) {
            conditionHash.put("(dcl.createDate <= ?{paramIndex}) ", DateTimeUtils.parse("yyyy-MM-dd HH:mm",endTime));
        }
        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(dcl) from DataCatLog dcl ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<DataCatLog> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select dcl from DataCatLog dcl ", "order by dcl.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<DataCatLog> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, DataCatLog.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public DataCatLog findModel(Long id) {
        return entityManager.find(DataCatLog.class, id);
    }

    @Override
    public Integer add(DataCatLog model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("DataCatLogDaoImpl添加dataCatLog成功！");
        return 1;
    }

    @Override
    public Integer update(DataCatLog model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        DataCatLog dataCatLog = entityManager.find(DataCatLog.class, id);
        entityManager.remove(dataCatLog);
        return 1;
    }

    @Override
    public Integer deleteLogic(DataCatLog model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
